﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DA;
using System.Text.RegularExpressions;
using Entities;

namespace BL
{
    public class UserManager
    {

        public IEnumerable<ERP_Users> GetAllUsers()
        {
            using (dotNetivotEntities model = new dotNetivotEntities())
            {
                return model.ERP_Users.ToList();
            }
        }

        public ERP_Users GetUser(int id)
        {
            using (dotNetivotEntities model = new dotNetivotEntities())
            {
                return model.ERP_Users.SingleOrDefault(x => x.UserId == id);
            }
        }

        public bool AddUser(ERP_Users user)
        {
            try
            {
                using (dotNetivotEntities model = new dotNetivotEntities())
                {
                    user.UserGuid = Guid.NewGuid();
                    model.ERP_Users.AddObject(user);
                    model.SaveChanges();
                }
            }
            catch
            {
                return false;
            }
            return true;
        }

        public bool UserExist(int id)
        {
            using (dotNetivotEntities model = new dotNetivotEntities())
            {
                return model.ERP_Users.Any(x => x.UserId == id);
            }
        }

        public bool EditUser(ERP_Users user)
        {
            try
            {
                using (dotNetivotEntities model = new dotNetivotEntities())
                {
                    ERP_Users userToUpdate = model.ERP_Users.SingleOrDefault(x => x.UserId == user.UserId);
                    userToUpdate.Password = user.Password;
                    userToUpdate.EMail = user.EMail;
                    userToUpdate.FirstName = user.FirstName;
                    userToUpdate.LastName = user.LastName;
                    userToUpdate.Roles = user.Roles;
                    userToUpdate.Permissions = user.Permissions;

                    model.SaveChanges();
                }
            }
            catch
            {
                return false;
            }

            return true;
        }

        public string ExportAllUsersToCSV()
        {
            List<ERP_Users> AllUsers;
            StringBuilder csvBuilder = new StringBuilder();

            using (dotNetivotEntities model = new dotNetivotEntities())
            {
                AllUsers = model.ERP_Users.ToList();
            }

            foreach (ERP_Users user in AllUsers)
            {
                csvBuilder.Append(user.UserId);
                csvBuilder.Append(',');
                csvBuilder.Append(user.EMail);
                csvBuilder.Append(',');
                csvBuilder.Append(user.Password);
                csvBuilder.Append(',');
                csvBuilder.Append(user.Roles);
                csvBuilder.Append(',');
                csvBuilder.Append(user.Permissions);
                csvBuilder.Append(',');
                csvBuilder.Append(user.FirstName);
                csvBuilder.Append(',');
                csvBuilder.Append(user.LastName);
                csvBuilder.Append(',');
                csvBuilder.Append(user.UserGuid);
                csvBuilder.Append(Environment.NewLine);
            }

            return csvBuilder.ToString();
        }
    }
}
